package com.txunda.zhuohired.fgt.user.find;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.view.inScroll.ListViewForScrollView;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.adapter.user.find.FindInformationAdapter;
import com.txunda.zhuohired.aty.user.find.FindInformationAty;
import com.txunda.zhuohired.base.BaseFgt;
import com.txunda.zhuohired.bean.user.find.FindBean;
import com.txunda.zhuohired.http.pst.MessagePst;
import com.txunda.zhuohired.view.ObservableScrollView;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/6 13:19
 * 功能描述：
 * 联系方式：jiahuanchen@126.com
 */

public class FindInformationFgt extends BaseFgt {
    /**
     * listView
     */
    @ViewInject(R.id.lv_find_information)
    private ListViewForScrollView lv_find_information;

    @ViewInject(R.id.obs_find_scroll_view)
    private ObservableScrollView obs_find_scroll_view;
    @ViewInject(R.id.ll_no_data)
    private LinearLayout ll_no_data;

    @ViewInject(R.id.ll_find_head)
    private LinearLayout ll_find_head;
    /**
     * 标题
     */
    @ViewInject(R.id.tv_find_information_tittle)
    private TextView tv_find_information_tittle;
    /**
     * 图片
     */
    @ViewInject(R.id.iv_find_information_tittle)
    private ImageView iv_find_information_tittle;
    /**
     * 分类
     */
    @ViewInject(R.id.tv_find_information_type)
    private TextView tv_find_information_type;
    /**
     * 浏览
     */
    @ViewInject(R.id.tv_find_information_look)
    private TextView tv_find_information_look;
    /**
     * 点赞
     */
    @ViewInject(R.id.tv_find_information_praise)
    private TextView tv_find_information_praise;

    private FindInformationAdapter findInformationAdapter;

    private List<FindBean.DataBean.TypeBean> tittleList;
    private int size = 0;
    private String cate_id = "1";
    private MessagePst messagePst;
    private List<FindBean.DataBean.ArticleBean> articleList;
    private List<FindBean.DataBean.ArticleOneBean> articleOneList;

    @Override
    protected void immersionInit() {

    }

    /**
     * 当Fragment显示是运行该方法
     *
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {

        if (hidden) {
            //界面不可见
        } else {
            // 界面可见
            initHeight();

        }
    }

    @Override
    @OnClick({R.id.ll_find_head})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_find_head:
                Bundle bundle = new Bundle();
                bundle.putString("cate_id", articleOneList.get(0).getId());
                startActivity(FindInformationAty.class, bundle);
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fgt_find_information;
    }

    @Override
    protected void initialized() {
        messagePst = new MessagePst(this);
        articleList = new ArrayList<>();
        articleOneList = new ArrayList<>();
    }

    private void initHeight() {
        lv_find_information.setFocusable(false);
        lv_find_information.setClickable(true);
        lv_find_information.setPressed(false);
        lv_find_information.setEnabled(true);

    }

    /**
     * 每次fragment可见时
     * <p>
     * 请求接口
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            new Handler().postDelayed(new Runnable() {

                public void run() {
                    cate_id = tittleList.get(size).getId();
                    //                    L.e("==========cate_id==========", cate_id);
                    messagePst.findHome(cate_id);
                }

            }, 100);

        } else {
            // 不可见时不执行操作
        }
    }

    @Override
    protected void requestData() {


        //        initAdapter();
    }

    private void initAdapter() {
        findInformationAdapter = new FindInformationAdapter(getActivity(), articleList);
        lv_find_information.setAdapter(findInformationAdapter);
        lv_find_information.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle = new Bundle();
                bundle.putString("cate_id", articleList.get(i).getId());
                startActivity(FindInformationAty.class, bundle);
            }
        });
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("findHome")) {
            FindBean findBean = GsonUtil.GsonToBean(jsonStr, FindBean.class);
            articleList = findBean.getData().getArticle();
            articleOneList = findBean.getData().getArticle_one();
            //            L.e("=====11111=====",jsonStr);
            if (articleList.size() == 0 && articleOneList.size() == 0) {
                noDate(1);
            } else {
                noDate(2);
                initAdapter();
                if (articleOneList.size() == 0) {
                    ll_find_head.setVisibility(View.GONE);
                } else {
                    ll_find_head.setVisibility(View.VISIBLE);
                    if (TextUtils.isEmpty(articleOneList.get(0).getTitle())) {
                        tv_find_information_tittle.setVisibility(View.GONE);
                    } else {
                        tv_find_information_tittle.setVisibility(View.VISIBLE);
                        tv_find_information_tittle.setText(articleOneList.get(0).getTitle());
                    }
                    Glide.with(getActivity()).load(articleOneList.get(0).getPic()).error(R.mipmap.icon_defult_bg)
                            .placeholder(R.mipmap.icon_defult_bg).dontAnimate().into(iv_find_information_tittle);
                    if (TextUtils.isEmpty(articleOneList.get(0).getType_name())) {
                        tv_find_information_type.setVisibility(View.GONE);
                    } else {
                        tv_find_information_type.setVisibility(View.VISIBLE);
                        tv_find_information_type.setText(articleOneList.get(0).getType_name());
                    }
                    if (TextUtils.isEmpty(articleOneList.get(0).getView())) {
                        tv_find_information_look.setVisibility(View.GONE);
                    } else {
                        tv_find_information_look.setVisibility(View.VISIBLE);
                        tv_find_information_look.setText(articleOneList.get(0).getView());
                    }
                    if (TextUtils.isEmpty(articleOneList.get(0).getGoods())) {
                        tv_find_information_praise.setVisibility(View.GONE);
                    } else {
                        tv_find_information_praise.setVisibility(View.VISIBLE);
                        tv_find_information_praise.setText(articleOneList.get(0).getGoods());
                    }
                }
            }
            return;
        }
    }

    public static FindInformationFgt getFgt(List<FindBean.DataBean.TypeBean> tittleList, int size) {
        FindInformationFgt findInformationFgt = new FindInformationFgt();
        findInformationFgt.tittleList = tittleList;
        findInformationFgt.size = size;
        return findInformationFgt;
    }

    private void noDate(int type) {
        if (type == 1) {
            obs_find_scroll_view.setVisibility(View.GONE);
            ll_no_data.setVisibility(View.VISIBLE);
        } else {
            obs_find_scroll_view.setVisibility(View.VISIBLE);
            ll_no_data.setVisibility(View.GONE);
        }
    }
}
